<template>
    <div>
        <el-dialog :title="isAdd ? '添加轮播图' : '编辑轮播图'" :visible.sync="isShow" center :before-close="cancel">
            <el-form :model="bannerForm" ref="bannerinfo" :rules="rules">
                <el-form-item prop="title" label="标题" :label-width="formLabelWidth">
                    <el-input v-model="bannerForm.title" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="图片" :label-width="formLabelWidth">
                    <el-upload 
                        action="#" 
                        list-type="picture-card"
                        :auto-upload="false"
                        :on-preview="onPreview"
                        :on-change="onChange"
                        :file-list="fileList"
                        :on-exceed="handleExceed"
                        :limit="1"
                    >
                        <i class="el-icon-plus"></i>
                    </el-upload>
                    <el-dialog :visible.sync="dialogVisible">
                        <img width="100%" :src="dialogImageUrl" alt="">
                    </el-dialog>
                </el-form-item>
                <el-form-item label="状态" :label-width="formLabelWidth">
                    <el-switch v-model="bannerForm.status" active-color="#13ce66" inactive-color="#ff4949"
                        :active-value="1" :inactive-value="2">
                    </el-switch>
                </el-form-item>
            </el-form>

            <div slot="footer" class="dialog-footer">
                <el-button @click="cancel">取 消</el-button>
                <el-button type="primary" @click="confirm">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { mapActions } from "vuex";
import {bannerAdd,bannerEdit,bannerInfo} from '../../request/api'
export default {
    props:['isShow','isAdd'],
    data() {
        return {
            formLabelWidth: '120px',
            bannerForm: {
                title: '',
                img: '',
                status:1
            },
            rules: {
                title: [
                    { required: true, message: "请输入分类名称", trigger: "blur" },
                    { min: 2, max: 6, message: "长度在 2 到 6 个字符", trigger: "blur" },
                ],
            },
            dialogVisible: false,//控制图片预览的显示隐藏
            dialogImageUrl: '',//图片地址
            fileList:[] //文件列表
        }
    },
    methods: {
        //超出文件上传的回调事件
        handleExceed(files, fileList) {
            this.$message.warning(`当前限制选择1个文件，本次选择了${files.length}个文件，共选择了${files.length+fileList.length}个文件`)
        },
        //图片改变时发生的事件
        onChange(file, fileList) {
            this.bannerForm.img = file.raw
        },
        //图片预览
        onPreview(file) { 
            //显示弹框
            this.dialogVisible = true
            this.dialogImageUrl = file.url
        },

        ...mapActions({
            getBannerAction: "banner/getBannerAction",
        }),
        cancel() {
            this.$emit('cancel', false)
            this.reset()
        },
        reset() {
            this.bannerForm = {
                title: '',
                img: '',
                status:1
            }
            this.$refs.bannerinfo.resetFields()
            this.fileList=[]
        },
        lookup(id) {
            bannerInfo({ id }).then(res => {
                if (res.code == 200) {
                    this.bannerForm = res.list
                    this.bannerForm.id = id
                    this.fileList = this.bannerForm.img?[{url:'http://localhost:3000'+this.bannerForm.img}]:[]
                }
            })
        },
        confirm() {
            this.$refs.bannerinfo.validate(val => {
                if (val) {
                    if (this.isAdd) {
                        bannerAdd(this.bannerForm).then(res => {
                            if (res.code == 200) {
                                this.$message.success(res.msg)
                                this.cancel()
                                this.getBannerAction()
                            } else {
                                this.$message.error(res.msg)
                            }
                        })
                    } else {
                        bannerEdit(this.bannerForm).then(res => {
                            if (res.code == 200) {
                                this.$message.success(res.msg)
                                this.cancel()
                                this.getBannerAction()
                            } else {
                                this.$message.error(res.msg)
                            }
                        })
                    }
                }
            })
        }
    }
}
</script>

<style scoped>

</style>